<!DOCTYPE html>
<html lang="en" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/layuiadmin/layui/css/layui.css">
</head>
<body>
    <div class="layui-fluid">
        <div id="searchParam">
            <div class="layui-form-item">
                <div class="layui-input-inline">
                    <input type="text" id="aid" class="layui-input" autocomplete="off" placeholder="用户ID">
                </div>
                <div class="layui-input-inline">
                    <input type="text" id="username" class="layui-input" autocomplete="off" placeholder="请输入账号">
                </div>
                <div class="layui-input-inline">
                    <input type="text" id="operation" class="layui-input" autocomplete="off" placeholder="用户操作动作">
                </div>
                <div class="layui-input-inline">
                    <input type="text" id="createTime" class="layui-input" autocomplete="off" placeholder="创建时间">
                </div>
                <div class="layui-input-inline ">
                    <button class="layui-btn" id="search">查询</button>
                </div>
            </div>
        </div>
        <table class="layui-hide" id="log_table" lay-filter="log_table"></table>
        <div id="laypage" class="layui-laypage-btn"></div>
    </div>
</body>
<script type="text/html" id="toolbar">
    <div class="layui-btn-group">
        <button type="button" class="layui-btn" lay-event="getCheckData">
            <i class="layui-icon layui-icon-delete"></i> 批量删除
        </button>
    </div>
</script>
<script type="text/html" id="tool">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
</script>
<script src="/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '/layuiadmin/'//静态资源所在路径
    }).extend({
        CoreUtil : 'core-util/CoreUtil'
    }).use(['table','laypage', 'layer','laydate','CoreUtil'], function(){
        var table = layui.table;
        var laypage = layui.laypage;
        var form = layui.form;
        var layer = layui.layer;
        var $ = jQuery = layui.jquery;
        var laydate = layui.laydate;
        var CoreUtil = layui.CoreUtil;

        var searchParam= {
            username:null,
            startTime:null,
            operation:null,
            aid:null,
            endTime:null,
            pageNum:1,
            pageSize:10
        };
        //渲染分页插件
        var laypageTable = function(count) {
            laypage.render({
                elem: 'laypage'
                ,count: count
                ,limit:searchParam.pageSize
                ,layout: ['count', 'prev', 'page', 'next', 'limit', 'skip']
                ,limits:[10, 20, 30, 40, 50]
                ,curr: location.hash.replace('#!currentPage=', '') //获取起始页
                ,hash: 'currentPage' //自定义hash值
                , jump: function (obj,first) {
                    if (!first){
                        searchParam.pageNum=obj.curr;
                        searchParam.pageSize=obj.limit;
                        getTableData(false);
                    }
                }
            });
        };

        //渲染table
        var loadTable=function (data) {
            table.render({
                elem: '#log_table'
                ,cols: [
                    [
                        {type: 'checkbox', fixed: 'left'},
                        {field: 'username', title: '账号'},
                        {field: 'operation', title: '用户操作'},
                        {field: 'method', title: '请求方法'},
                        {field: 'params', title: '请求参数'},
                        {field: 'createTime', title: '创建时间'},
                        {width:150,toolbar:"#tool",title:'操作'}
                    ]
                ]
                ,data: data
                ,even: true
                ,limit: data.length
                ,limits: [10, 20, 30, 40, 50]
                ,toolbar:'#toolbar'
            });
        };

        /**
         * 获取分页数据
         */
        var getTableData = function(flag){
            CoreUtil.sendAjax("/manager/sys/logs",searchParam,function (res) {
                if(flag){laypageTable(res.data.totalElements);}
                loadTable(res.data.content);
            },"GET",false,false);
        };
        getTableData(true);
        //日期范围
        laydate.render({
            elem: '#createTime'
            ,type: 'datetime'
            ,range: '~'
            ,done: function(value, date, endDate){
                if(value !=null && value != undefined && value != ""){
                    searchParam.startTime=value.split("~")[0];
                    searchParam.endTime=value.split("~")[1];
                }else {
                    //清空时间的时候要清空以前选择的日期
                    searchParam.startTime=null;
                    searchParam.endTime=null;
                }
            }
        });
        //执行查询
        $("#search").click(function () {
            searchParam.aid=$("#aid").val();
            searchParam.operation=$("#operation").val();
            searchParam.username=$("#username").val();
            searchParam.pageNum=1;
            getTableData(true);
        });

        //表头工具栏事件
        table.on('toolbar(log_table)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'getCheckData':
                    var data = checkStatus.data;
                    if(data.length==0){
                        layer.msg("请选择要批量删除的日志");
                    }else {
                        var logIds = [];
                        $(data).each(function (index,item) {
                            logIds.push(item.id);
                        });
                        tipDialog(logIds);
                    }
                    break;
            };
        });
        //操作工具栏事件
        table.on('tool(log_table)',function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'del':
                    var logIds=[];
                    logIds.push(data.id);
                    tipDialog(logIds);
                    break;
            }
        });

        //删除前确认对话框
        var tipDialog=function (logIds) {
            layer.open({
                content: '确定要删除么',
                yes: function(index, layero){
                    layer.close(index); //如果设定了yes回调，需进行手工关闭
                    CoreUtil.sendAjax("/manager/sys/logs",JSON.stringify(logIds),function (res) {
                        top.layer.msg(res.msg, {
                            offset: 't',
                            anim: 6,
                            time:1500
                        }, function(){
                            getTableData();
                        });
                    },"DELETE",'application/json; charset=UTF-8');
                }
            });
        }
    });
</script>
</html>